package self.hoy.demo.grainmall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import self.hoy.demo.common.utils.PageUtils;
import self.hoy.demo.common.utils.Query;

import self.hoy.demo.grainmall.product.entity.Brand;
import self.hoy.demo.grainmall.product.entity.Category;
import self.hoy.demo.grainmall.product.mapper.BrandMapper;
import self.hoy.demo.grainmall.product.mapper.CategoryBrandRelationMapper;
import self.hoy.demo.grainmall.product.entity.CategoryBrandRelation;
import self.hoy.demo.grainmall.product.mapper.CategoryMapper;
import self.hoy.demo.grainmall.product.service.CategoryBrandRelationService;

import javax.annotation.Resource;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationMapper, CategoryBrandRelation> implements CategoryBrandRelationService {

    @Resource
    private BrandMapper brandMapper;

    @Resource
    private CategoryMapper categoryMapper;

    @Resource
    private CategoryBrandRelationMapper relationMapper;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelation> page = this.page(
                new Query<CategoryBrandRelation>().getPage(params),
                new QueryWrapper<CategoryBrandRelation>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveDetail(CategoryBrandRelation categoryBrandRelation) {
        Long brandId = categoryBrandRelation.getBrandId();
        Long categoryId = categoryBrandRelation.getCatelogId();
        Brand brand = brandMapper.selectById(brandId);
        Category category = categoryMapper.selectById(categoryId);
        categoryBrandRelation.setBrandName(brand.getName());
        categoryBrandRelation.setCatelogName(category.getName());
        this.save(categoryBrandRelation);
    }

    @Override
    public void updateBrand(Long brandId, String brandName) {
        CategoryBrandRelation categoryBrandRelation = new CategoryBrandRelation();
        categoryBrandRelation.setBrandId(brandId);
        categoryBrandRelation.setBrandName(brandName);
        this.update(
                categoryBrandRelation,
                new UpdateWrapper<CategoryBrandRelation>().eq("brand_id", brandId)
        );
    }

    @Override
    public void updateCategory(Long categoryId, String categoryName) {
        baseMapper.updateCategory(categoryId, categoryName);
    }

    @Override
    public List<Brand> getBrandsByCatId(Long catId) {
        List<CategoryBrandRelation> relations = relationMapper.selectList(
                new QueryWrapper<CategoryBrandRelation>().eq("catelog_id", catId)
        );
        List<Long> brandIsList = relations == null ?
                null :
                relations.stream().map(r->{
                    return r.getBrandId();
                }).collect(Collectors.toList());
        List<Brand> brands = brandIsList == null || brandIsList.isEmpty() ?
                null :
                brandMapper.selectList(
                        new QueryWrapper<Brand>()
                                .in("brand_id", brandIsList)
                );

        return brands;
    }

}